package com.pet.platform.controller.admin;

import com.pet.platform.common.PageResult;
import com.pet.platform.common.Result;
import com.pet.platform.dto.AppointmentDTO;
import com.pet.platform.dto.AppointmentQueryDTO;
import com.pet.platform.entity.Appointment;
import com.pet.platform.service.AppointmentService;
import com.pet.platform.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/admin/appointments")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class AdminAppointmentController {
    
    private final AppointmentService appointmentService;
    private final UserMapper userMapper;
    
    /**
     * 获取预约列表
     */
    @GetMapping
    public Result<PageResult<AppointmentDTO>> getAppointments(AppointmentQueryDTO queryDTO) {
        PageResult<AppointmentDTO> pageResult = appointmentService.findPage(queryDTO);
        return Result.success(pageResult);
    }
    
    /**
     * 获取预约详情
     */
    @GetMapping("/{id}")
    public Result<AppointmentDTO> getAppointmentDetail(@PathVariable Long id) {
        Appointment appointment = appointmentService.findById(id);
        AppointmentDTO appointmentDTO = convertToDTO(appointment);
        return Result.success(appointmentDTO);
    }
    
    /**
     * 将Appointment转换为AppointmentDTO
     */
    private AppointmentDTO convertToDTO(Appointment appointment) {
        if (appointment == null) {
            return null;
        }
        
        AppointmentDTO dto = new AppointmentDTO();
        // 复制基本属性
        dto.setId(appointment.getId());
        dto.setUserId(appointment.getUserId());
        dto.setServiceId(appointment.getServiceId());
        dto.setPetId(appointment.getPetId());
        dto.setAppointmentTime(appointment.getAppointmentTime());
        dto.setStatus(appointment.getStatus());
        dto.setRemark(appointment.getRemark());
        
        // 如果appointment中有关联的实体信息，也复制过来
        if (appointment.getService() != null) {
            dto.setServiceName(appointment.getService().getName());
        }
        
        if (appointment.getPet() != null) {
            dto.setPetName(appointment.getPet().getName());
        }
        
        // 获取用户名
        if (appointment.getUserId() != null) {
            com.pet.platform.entity.User user = userMapper.findById(appointment.getUserId());
            if (user != null) {
                dto.setUsername(user.getUsername());
            }
        }
        
        return dto;
    }
    
    /**
     * 更新预约状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateAppointmentStatus(@PathVariable Long id, @RequestParam String status) {
        appointmentService.updateStatus(id, status);
        return Result.success();
    }
    
    /**
     * 删除预约
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteAppointment(@PathVariable Long id) {
        appointmentService.delete(id);
        return Result.success();
    }
} 